package com.wanpinghui.supplier.product.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.product.bean.ProductGroupBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;

import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/20
 ***/

public class ProductGroupAdapter extends BaseRecyclerViewAdapter<ProductGroupBean, ProductGroupAdapter.GroupHolder> {

    public ProductGroupAdapter(Context context) {
        super(context);
    }

    @Override
    public void setDatas(List<ProductGroupBean> datas) {
        this.datas = datas;
        if (datas != null && datas.size() > 0) {
            datas.get(0).setSelect(true);
        }
        notifyDataSetChanged();
    }

    public void setSelected(int position) {
        if (position < getItemCount()) {
            for (int i = 0; i < datas.size(); i++) {
                if (position == i) {
                    datas.get(i).setSelect(true);
                } else {
                    datas.get(i).setSelect(false);
                }
            }
            notifyDataSetChanged();
        }
    }

    public ProductGroupBean getData(int position) {
        if (position < getItemCount()) {
            return datas.get(position);
        }
        return null;
    }

    @Override
    public GroupHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recv_product_group, parent, false);
        return new GroupHolder(view);
    }

    @Override
    public void onBindViewHolder(GroupHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ProductGroupBean bean = datas.get(position);
        holder.textViewName.setText(bean.getName());
        TextPaint paint = holder.textViewName.getPaint();
        //  holder.viewLine.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DevicesUtil.dip2px(context,4f)));
        if (bean.isSelect()) {
            holder.textViewName.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryBlue));

            paint.setFakeBoldText(true);
            holder.viewLine.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimaryBlue));

        } else {
            paint.setFakeBoldText(false);
            holder.textViewName.setTextColor(ContextCompat.getColor(context, R.color.color_d2d9e0));
            holder.viewLine.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class GroupHolder extends RecyclerView.ViewHolder {
        TextView textViewName;
        View viewLine;

        public GroupHolder(View itemView) {
            super(itemView);
            textViewName = (TextView) itemView.findViewById(R.id.tv_name);
            viewLine = itemView.findViewById(R.id.view_line);
        }
    }
}
